/**
 * Created by dell on 2017/12/4.
 */
// document.write("<script src='https://cdn.bootcss.com/vue/2.5.9/vue.js'><\/script>");
import $axios from 'axios'
import $router from '../router';
import qs from 'qs';
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import { Loading } from 'element-ui';
import { Message } from 'element-ui';
import { MessageBox } from 'element-ui';

function validateData(value) {
  if (value == null || value == undefined || value == '') {
    return false
  }
  return true;
}

export {
  validateData,
}
var utils = {
  validateData: function(value) {
    if (value == null || value == undefined || value == '') {
      return false
    }
    return true;
  },
  showWarning: function(text) {
    var option = {
      message: text,
      type: 'warning',
      center: true
    }
    Message(option)
  },
  requestLoading: function(url, params, success, fail, complete) {
    let loadingInstance = Loading.service({ fullscreen: true });
    var success2 = function(data) {
      loadingInstance.close();
      success(data)
    }
    var fail2 = function() {
      loadingInstance.close();
      fail()
    }
    var complete2 = function() {
      loadingInstance.close();
      complete()
    }
    this.request(url, params, success2, fail2, complete2)
  },
  request: function(url, params, success, fail, complete) {
    var self = this
    var axios = require("axios")
    if (utils.validateData(params)) {
      params['vue'] = 1;
    } else {
      params = { vue: 1 }
    }
    axios.post(url, qs.stringify(params))
      .then(response => {
        var data = response.data;
        var status = data.status
        if (status == 200) {
          success(data)
        } else if (status == 203) {
          complete()
          self.showWarning(data.msg)
        } else if (status == 204) {
          complete()
          self.showWarning(data.msg)
        } else if (status == 401) {
          $router.push({ name: 'Login' })
          complete()
        } else if (!utils.validateData(status)) {
          success(data)
        } else {
          fail()
        }
      })
      .catch(err => {
        console.log(err);
        fail()
      });
  },
  fail: function(msg) {
    MessageBox.alert(msg, '提示', {
      confirmButtonText: '确定',
      type: 'error'
    });
  },
  del: function(event, msg) {
    MessageBox.confirm(msg == null ? '删除此记录, 是否继续?' : msg, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      event()
    }).catch(() => {});
  },
  dateFormat: function(date) {
    Date.prototype.Format = function(fmt) {
      var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
      };
      if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      return fmt;
    }
    var dateFmt = date.Format("yyyy-MM-dd");
    return dateFmt;
  },
  setCookie: function(c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + escape(value) +
      ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
  },
  getCookie: function(c_name) {
    if (document.cookie.length > 0) {
      var c_start = document.cookie.indexOf(c_name + "=")
      if (c_start != -1) {
        c_start = c_start + c_name.length + 1
        var c_end = document.cookie.indexOf(";", c_start)
        if (c_end == -1) c_end = document.cookie.length
        return unescape(document.cookie.substring(c_start, c_end))
      }
    }
    return ""
  }

}
export default utils
